package com.zhongyouyun.utils;

import cn.hutool.core.util.ObjectUtil;

import java.util.*;
import java.util.stream.Collectors;

public class MapUtils {
    public static List<HashMap<Object, Object>> compareListHitData(List<HashMap<Object, Object>> oneList, List<HashMap<Object, Object>> twoList, String name) {
        List<HashMap<Object, Object>> resultList = new ArrayList<HashMap<Object, Object>>();
        resultList = oneList.stream().map(map -> twoList.stream()
                .filter(m -> Objects.equals(m.get(name), map.get(name)))
                .findFirst().map(m -> {
                    map.putAll(m);
                    return map;
                }).orElse(map))
                .filter(Objects::nonNull).collect(Collectors.toList());
        return resultList;
    }

    public static List<HashMap<Object, Object>> compareListNullHitData(List<HashMap<Object, Object>> oneList, List<HashMap<Object, Object>> twoList, String name) {
        List<HashMap<Object, Object>> resultList = new ArrayList<HashMap<Object, Object>>();
        resultList = oneList.stream().map(map -> twoList.stream()
                .filter(m -> Objects.equals(m.get(name), map.get(name)))
                .findFirst().map(m -> {
                    map.putAll(m);
                    return map;
                }).orElse(null))
                .filter(Objects::nonNull).collect(Collectors.toList());
        return resultList;
    }

    public static List<Map<String, Integer>> compareMapHit(List<Map<String, Integer>> oneList, Map<String, String> twoMap) {
        List<Map<String, Integer>> resultList = new ArrayList<>();
        for (Map map:oneList){
            map.forEach((k, v) -> {
                Map<String, Integer> hashMap = new HashMap<String, Integer>();
                if (ObjectUtil.isNotEmpty(twoMap.get(k))) {
                    hashMap.put(k.toString(), Integer.valueOf(twoMap.get(k).toString()));
                } else {
                    hashMap.put(k.toString(), (Integer) v);
                }
                resultList.add(hashMap);
            });
        }
        return resultList;
    }

}
